package com.wyl.coroutinesapp

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.wyl.coroutinesapp.util.LogUtil
import kotlinx.android.synthetic.main.fragment_demo1.*
import kotlinx.coroutines.*
import kotlin.time.measureTime

/**
 * 挂起协程另一种方式: delay()
 */
@kotlin.time.ExperimentalTime
@SuppressLint("SetTextI18n")
class Demo1Fragment : Fragment() {
    private val mainScope = MainScope()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_demo1, container, false)
    }


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        button.setOnClickListener {
            textView.text = "获取用户信息中..."
            mainScope.launch {
                measureTime {
                    val token = token()
                    val userInfo = userInfo(token)
                    textView.text = textView.text.toString() + "\n$userInfo"
                }.also(LogUtil::d)
                it.isClickable = true
            }
            it.isClickable = false
        }

        button2.setOnClickListener {
            textView.text = "支付中..."
            mainScope.launch {
                measureTime {
//                    function1()
                    function2()
                }.also(LogUtil::d)
                it.isClickable = true
            }
            it.isClickable = false
        }
    }

    // 用时9秒
    private suspend fun function2() {
        val userInfo = mainScope.async { userInfo(token()) }
        val token2 = mainScope.async { token2() }
        val payResult = pay(token2.await(), userInfo.await())
        textView.text = textView.text.toString() + "\n${payResult}"
        textView.text = textView.text.toString() + "\nend"
    }

    // 用时14秒
    private suspend fun function1() {
        val token = token()
        val userInfo = userInfo(token)
        val token2 = token2()
        val payResult = pay(token2, userInfo)
        textView.text = textView.text.toString() + "\n$payResult"
        textView.text = textView.text.toString() + "\nend"
    }

    private suspend fun token(): String {
        textView.text = textView.text.toString() + "\n获取token..."
        return withContext(Dispatchers.Default) {
            Thread.sleep(2000)
            "token"
        }
    }

    private suspend fun token2(): String {
        textView.text = textView.text.toString() + "\n获取token2..."
        return withContext(Dispatchers.Default) {
            Thread.sleep(5000)
            "token2"
        }
    }

    private suspend fun userInfo(token: String): String {
        textView.text = textView.text.toString() + "\n获取userInfo..."
        return withContext(Dispatchers.Default) {
            Thread.sleep(3000)
            if (token == "token") {
                "userInfo"
            } else {
                "token is wrong!!!"
            }
        }
    }

    private suspend fun pay(token2: String, userInfo: String): String {
        textView.text = textView.text.toString() + "\npaying..."
        return withContext(Dispatchers.Default) {
            Thread.sleep(4000)
            when {
                token2 != "token2" -> "token is wrong!!!"
                userInfo != "userInfo" -> "userInfo is wrong!!!"
                else -> "pay success"
            }
        }

    }

    override fun onDestroy() {
        mainScope.cancel()
        super.onDestroy()
    }

}
